package Cluster;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import Application.ImageGenerator;
import Application.ReadFileGenerateor;
import MyLoader.MyLoader;
import VectorApproach.TrajVector;
import VectorApproach.VClusterBulk;
import XmlLoader.FGObjProcess;
import XmlLoader.FGObjectTrajectoryStructure;

public class ClusterReader {
	
	public static ArrayList<ClusterBulk> loadFromFile(ArrayList<Trajectory> list, String filePathRead) {
		String readStr = null;
		ClusterBulk cb = null;
		int getTrajID = 0, getSegID = 0;
		ArrayList<ClusterBulk> listCb = new ArrayList<ClusterBulk>();
		try {
			BufferedReader br = new BufferedReader(new FileReader(filePathRead));
			while((readStr = br.readLine()) != null) {
				if(readStr.startsWith("C")) {
					if(cb != null) {
						listCb.add(cb);
					}
					cb = new ClusterBulk(Integer.parseInt(readStr.substring(1)), -1);
				}
				else {
					getTrajID = Integer.parseInt(readStr);
					int index = 0;
					boolean isFound = false;
					Trajectory getTraj = null;
					while(!isFound && index < list.size()) {
						Trajectory trajV = list.get(index);
						if(trajV.GET003_trajID() == getTrajID) {
							getTraj = trajV;
							isFound = true;
						}
						index = index + 1;
					}
					cb.ACT001_insertNewTraj(getTraj);
				}
			}
			listCb.add(cb);
			//ImageGenerator.GenClusterOneByOne(filePathWrite, listCb, length, width, background);
		
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return listCb;
	}

	public static void main(String[] args) {
		//generate list trajectory index
		System.out.println("Generate trajectory index file");
		new ReadFileGenerateor().ACT001_genFile(args[0], Integer.parseInt(args[1]), Integer.parseInt(args[2]));
												
		//load trajectory xml data
		System.out.println("Load trajectory xml file");
		FGObjProcess fgObj = new FGObjProcess(args[3]);
		List<String> list_objIndex = fgObj.ACT004_getStringList(args[0]);
		List<FGObjectTrajectoryStructure> list = fgObj.ACT001_getListOfObj(list_objIndex);
		
		//trajectory structure transformation
		ArrayList<Trajectory> listTraj = new ArrayList<Trajectory>();
		int index = 0;
		while(index < list.size()) {
			listTraj.add(new Trajectory(list.get(index), 0, 0, list.get(index).GET002_nNumberOfPoint()));
			//list.remove(index);
			index = index + 1;
		}
		
		ArrayList<ClusterBulk> listCb = ClusterReader.loadFromFile(listTraj, "C:\\Users\\test\\Desktop\\crossroad_lcss\\330.txt");
		ImageGenerator.GenCluster("C:\\Users\\test\\Desktop\\show.png", listCb, 320, 240, "C:\\Users\\test\\Desktop\\crossroad\\0.jpg");
	}
}
